package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.util.ImageUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.BeanEos;
import com.sskj.mine.asset.data.RechargeBean;
import com.sskj.mine.bean.ReWithCoinBean;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/05/03
 */
@Route(path = RConfig.MINE_RECHARGE)//充币
public class RechargeActivity extends BaseActivity<RechargePresenter> {

    @BindView(R2.id.tvTitle)
    HeySpinner selectCoin;
    @BindView(R2.id.qr_code)
    ImageView qrCode;
    @BindView(R2.id.address)
    TextView address;
    @BindView(R2.id.copy_tv)
    TextView copyTv;
    @BindView(R2.id.ivRecord)
    ImageView rightTv;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.tvSaveQR)
    TextView tvSaveQR;

    private Disposable typeDispo;
    private String type;
    private BottomSheetDialog bottomSheetCoin;
    private String code;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    public void initView() {
        selectCoin.setItemGravity(Gravity.CENTER);
        ClickUtil.click(rightTv, () -> ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS).withInt(Constans.TYPE, 1).navigation());

    }

    @Override
    public void initData() {
//        mPresenter.getType();
        type="2";
        code="USDT";
        mPresenter.recharge(type, code);

    }

    public void refresh() {
        mPresenter.recharge(type, code);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
        context.startActivity(intent);
    }

    public void updateType(List<ReWithCoinBean> data) {
        if (data == null || data.size() <= 0)
            return;
        type = data.get(0).getStockType();
        code = data.get(0).getStockCode();
        if (!"EOS".equalsIgnoreCase(code)) {
            selectCoin.setText(code);
            tvDesc.setText(String.format(App.INSTANCE.getString(R.string.minerechargeActivity1), code, code, code, code));
            refresh();
        } else {
            mPresenter.checkEosAccount();
        }
        typeDispo = Flowable.fromIterable(data)
                .map(reWithCoinBean -> reWithCoinBean.getStockCode())
                .toList()
                .subscribe((strings, throwable) -> {
//                    selectCoin.setClickListener(() -> {
//                        selectCoin.animateArrow(true);
//                        bottomSheetCoin = BottomSheetUtil.getBottomSheet(RechargeActivity.this, null, (recyclerView, position, view) -> {
//                            bottomSheetCoin.dismiss();
//                            type = data.get(position).getStockType();
//                            code = data.get(position).getStockCode();
//                            selectCoin.setText(code);
//                            tvDesc.setText(String.format("App.INSTANCE.getString(R.string.minerechargeActivity1)", code, code, code, code));
//                            refresh();
//                        }, strings);
//                        bottomSheetCoin.show();
//                        return false;
//                    });

                    ClickUtil.click(selectCoin, () -> {
                        selectCoin.animateArrow(true);
                        bottomSheetCoin = BottomSheetUtil.getBottomSheet(RechargeActivity.this, null, (recyclerView, position, view) -> {
                            bottomSheetCoin.dismiss();
                            type = data.get(position).getStockType();
                            code = data.get(position).getStockCode();

                            if (!code.equals(selectCoin.getText())) {
                                if ("EOS".equalsIgnoreCase(code)) {
                                    mPresenter.checkEosAccount();
                                } else {
                                    refresh();
                                    selectCoin.setText(code);
                                    tvDesc.setText(String.format(App.INSTANCE.getString(R.string.minerechargeActivity1), code, code, code, code));
                                }
                            }

                        }, strings);
                        bottomSheetCoin.show();
                        bottomSheetCoin.setOnDismissListener(dialog -> {
                            if (selectCoin != null) {
                                selectCoin.animateArrow(false);
                            }
                        });

                    });


//                    selectCoin.attachData(strings);
//                    selectCoin.setOnSelectListener(position -> {
//                        pid = data.get(position).getPid();
//                        coinType = data.get(position).getPname();
//                        refresh();
//                    });

                });
    }

    public void updateUI(RechargeBean data) {
        address.setText(App.INSTANCE.getString(R.string.minerechargeActivity2) + data.getAddr());
        ImageUtil.setImage(data.getCodeUrlImg(), qrCode);
        ClickUtil.click(copyTv, () -> {
            CopyUtil.copy(data.getAddr());
        });
        ClickUtil.click(tvSaveQR, () -> {
            ImgUtil.saveImageToGallery(this, qrCode);
        });
    }


    public void checkEosAccountCallback(BeanEos beanEos) {
        //已有账户
        if (beanEos != null) {
            if (!TextUtils.isEmpty(beanEos.getAccount())) {
                refresh();
                selectCoin.setText(code);
                tvDesc.setText(String.format(App.INSTANCE.getString(R.string.minerechargeActivity1), code, code, code, code));

            } else {
                //没有账户
                TipUtil.tipsDialogChargeCoin(this, new TipUtil.OnTipSure3() {
                    @Override
                    public void onSure(String account, String paswd) {
                        ToastUtil.showShort(account);
                        mPresenter.createEosAccount(account, paswd);
                    }

                    @Override
                    public void onCancel() {

                    }
                }, beanEos.getUsdtNum());
            }

        }


    }

    public void createEosAccountCallback(String account) {
        //创建成功
        if (!TextUtils.isEmpty(account)) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.minerechargeActivity3));
        } else {

        }
    }
}
